package com.kfm.service.impl;

import com.kfm.entity.User;
import com.kfm.ex.ServiceException;
import com.kfm.mapper.UserMapper;
import com.kfm.service.UserService;
import com.kfm.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    @Override
    public User login(User user) {
     if (ObjectUtils.isEmpty(user) || !StringUtils.hasLength(user.getUsername()) ||
             !StringUtils.hasLength(user.getPassword())){
         throw new ServiceException("用户名或密码不能为空");
     }
     //密码加密
       user.setPassword(MD5Util.encode(user.getPassword()));
        List<User> users = mapper.selectAll(user);
        //如果users的大小大于0，说明查询到用户，返回第一个查询结果，否则返回null
        return users.size()>0 ? users.get(0) : null;
    }

    @Override
    public List<User> fandAll(User user) {
        List<User> users = mapper.selectAll(user);
        return users;
    }

    @Override
    public User delete(String id) {
        User delete = mapper.delete(id);
        return delete;
    }

    @Override
    public User save(User user) {
        return mapper.save(user);
    }


}
